package com.twotiger.and.web;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.webkit.WebView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.TwoTigerApp;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.WebViewDialogPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.project.ProjectDetailPage;
import com.twotiger.and.activity.user.LoginPage;
import com.twotiger.and.bean.Share;
import com.twotiger.and.bean.User;

/**
 * 被JS调用的方法
 * @author gao
 *
 */
public class JavaScriptInterfaceForDialog {
    protected static final int LOGINSUCCES = 0;
    protected static final int LOGINFAIL = 1;
    private Activity mcontext;
    private WebView mwebView;
    private BaseHandler mHandler;

    public static String name() {
        return "_TT";
    }

    public JavaScriptInterfaceForDialog(WebView webView, Activity context, BaseHandler handler) {
        super();
        mwebView = webView;
        mcontext = context;
        mHandler = handler;
    }

    /**
     * 注册成功页面，点击先逛逛
     */
    public void firstLook() {
        ((BaseActivity) mcontext).startNewActivity(new Intent(mcontext, HomeActivity.class),
            R.anim.push_left_in, R.anim.push_left_out, true);
    }

    /**
     * 注册成功页面，点击开通托管
     */
    public void registHF() {
        ((WebViewDialogPage) mcontext).isFinish = true;
        ((WebViewDialogPage) mcontext).openpayment();
    }

    /**
     * 进入注册页面
     */
    public void toRegist() {
//        Intent intent1 = new Intent(mcontext, RegisterPage.class);
//        ((WebViewDialogPage) mcontext).startNewActivity(intent1, R.anim.slide_in_from_bottom,
//            R.anim.silent_anim, false);
        Intent intent1 = new Intent(mcontext, LoginPage.class);
        intent1.putExtra("witchLayout", "regist");
        ((WebViewDialogPage) mcontext).startNewActivity(intent1, R.anim.slide_in_from_bottom,
            R.anim.silent_anim, false);
    }

    /**
     * 进入登录页面
     */
    public void toLogin() {
        Intent intent2 = new Intent(mcontext, LoginPage.class);
        ((WebViewDialogPage) mcontext).startNewActivity(intent2, R.anim.slide_in_from_bottom,
            R.anim.silent_anim, false);
    }

    /**
     * 进入精选、项目列表、我的钱、更多
     * @param tag  0-精选  1-项目列表   2-我的钱   3-更多
     */
    public void toPage(int tag) {
        switch (tag) {
        case 0:
            mHandler.sendEmptyMessage(WebViewDialogPage.GOTO_BOUTIQUEPAGE);
            break;
        case 1:
            mHandler.sendEmptyMessage(WebViewDialogPage.GOTO_PROJECTEPAGE);
            break;
        case 2:
            mHandler.sendEmptyMessage(WebViewDialogPage.GOTO_ACCOUNTEPAGE);
            break;
        case 3:
            mHandler.sendEmptyMessage(WebViewDialogPage.GOTO_MOREPAGE);
            break;
        default:
            break;
        }
    }

    /**
     * 进入某个项目的详情页面
     * 
     * @param projectId   
     * @param mode   
     */
    public void toProjectDetail(String type, String mode, String projectId) {
        if (Constants.TYPE_NID_ZHITOU.equals(type)) {
            Intent intent = new Intent(mcontext, ProjectDetailPage.class).putExtra("PROJECT_ID",
                projectId).putExtra("MODE", mode);
            ((WebViewDialogPage) mcontext).startNewActivity(intent, R.anim.push_left_in,
                R.anim.push_left_out, false);
        } else if (Constants.TYPE_NID_HEIDONG.equals(type)) {
        }
    }

    /**
     * 进入某个项目的投资页面
     * 
     * @param projectId
     * @param mode
     */
    public void toProjectInvest(String type, String mode, String projectId) {
//        由于投资页面需要对应的信息，故先请求网络拿到项目的详情信息
        ((WebViewDialogPage) mcontext).getProjectDetail(type, mode, projectId);
    }

    /**
     * 
     * 调用分享功能
     * @param url    
     * @param title
     * @param cont
     * @param img
     */
    public void toShare(String title,String cont,String url, String img) {
        
        Share mShare = new Share();
        mShare.setTitle(title);
        mShare.setContent(cont);
        mShare.setUrl(url);
        mShare.setPic(img);
        
        Message msg = Message.obtain();
        msg.what = WebViewDialogPage.TO_SHARE;
        msg.obj = mShare;
        mHandler.sendMessage(msg);
    }
    
    /**
     * 获取用户信息
     * @return
     */
    public void userInfo() {
        User user = null;
        if (((WebViewDialogPage) mcontext).isLogin()) {
            user = TwoTigerApp.getInstance().getUser();
        }
        final HashMap<String, String> hashMap = new HashMap<String, String>();
        if (user != null) {
            hashMap.put("name", user.name);
            hashMap.put("phone", user.phone);
            hashMap.put("thirdAccount", user.thirdAccountName);
            hashMap.put("token", ((WebViewDialogPage) mcontext).getToken()); //用户信息增加token
        }else {
            hashMap.put("name", "");
            hashMap.put("phone", "");
            hashMap.put("thirdAccount", "");
            hashMap.put("token", ""); 
        }
        mwebView.post(new Runnable() {
            @Override
            public void run() {
                String format = String.format("javascript:userInfoCallBack("
                                              + JSONObject.toJSONString(hashMap).toString() + ")");
                mwebView.loadUrl(format);
            }
        });
    };
    

}
